package com.cskaoyan.market.service.admin;

import com.cskaoyan.market.db.domain.MarketPermission;
import com.cskaoyan.market.db.domain.MarketPermissionExample;
import com.cskaoyan.market.db.domain.MarketRole;
import com.cskaoyan.market.db.domain.MarketRoleExample;
import com.cskaoyan.market.db.mapper.MarketPermissionMapper;
import com.cskaoyan.market.db.mapper.MarketRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.relation.Role;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @Author: chenjian
 * @Date: 2024/5/22 19:52
 */
@Service
public class MarketPermissionServiceImpl implements MarketPermissionService{
    @Autowired
    MarketPermissionMapper marketPermissionMapper;
    @Autowired
    MarketRoleMapper marketRoleMapper;

    @Override
    public Map<String,List<String>> getPermissionsAndRoles(Integer[] roleIds) {
        Map<String,List<String>> permissionsAndRoles = new HashMap<String,List<String>>();
        HashSet<String> permissions = new HashSet<>();
        List<String> roles = new ArrayList<>();
        MarketPermissionExample marketPermissionExample = new MarketPermissionExample();
        MarketRoleExample marketRoleExample=new MarketRoleExample();
        for (Integer roleId : roleIds) {
            MarketPermissionExample.Criteria criteria = marketPermissionExample.createCriteria();
            MarketRoleExample.Criteria criteria1 = marketRoleExample.createCriteria();
            criteria1.andIdEqualTo(roleId);
            criteria.andRoleIdEqualTo(roleId);
            List<MarketPermission> marketPermissions = marketPermissionMapper.selectByExample(marketPermissionExample);
            MarketRole marketRole = marketRoleMapper.selectOneByExample(marketRoleExample);
            roles.add(marketRole.getName());
            for (MarketPermission marketPermission : marketPermissions) {
                permissions.add(marketPermission.getPermission());
            }
        }
        permissionsAndRoles.put("perms",new ArrayList<>(permissions)) ;
        permissionsAndRoles.put("roles",roles);
        return permissionsAndRoles;
    }

    @Override
    public Boolean updatePermissions(String roleId, List<String> permissions) {
        MarketPermissionExample marketPermissionExample = new MarketPermissionExample();
        MarketPermissionExample.Criteria criteria = marketPermissionExample.createCriteria();
        criteria.andRoleIdEqualTo(Integer.valueOf(roleId));
        marketPermissionMapper.deleteByExample(marketPermissionExample);
        try {
            for (String permission : permissions) {
                MarketPermission marketPermission = new MarketPermission();
                marketPermission.setPermission(permission);
                marketPermission.setRoleId(Integer.valueOf(roleId));
                marketPermission.setDeleted(false);
                marketPermission.setAddTime(LocalDateTime.now());
                marketPermission.setUpdateTime(LocalDateTime.now());
                marketPermission.setPermission(permission);
                marketPermission.setRoleId(Integer.valueOf(roleId));
                marketPermissionMapper.insert(marketPermission);
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return false;
    }
}
